﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;

namespace ExaminationSys
{
    public partial class Helpdetail : System.Web.UI.Page
    {
        private static readonly StudentService Proxy = ObjectFactory<StudentService>.Create();
        private static readonly TeacherInfoService ProxyTeacherInfo = ObjectFactory<TeacherInfoService>.Create();
        private static readonly NewsTypeService ProxyNewsTypeService = ObjectFactory<NewsTypeService>.Create();
        private static readonly NewsService ProxyNewsService = ObjectFactory<NewsService>.Create();
        public News CurrentNews = new News();
        public Guid ID = Guid.Empty;
        public string TeacherName { get; set; }
        public StringBuilder htmlStr = new StringBuilder();
        public StringBuilder htmlLoginBox = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            var idTmp = Request["id"];
            if (!Guid.TryParse(idTmp, out ID))
            {
                Response.Redirect("error.aspx");
                Response.End();
            }
            if (!IsPostBack)
            {
                CurrentNews = ProxyNewsService.Get(ID);

                var teacher = ProxyTeacherInfo.GetTeacherInfoByUserId(CurrentNews.Operator);

                TeacherName = teacher == null ? "未知" : teacher.UserName;
                var loginInfo = Session["StudentInfo"] as Student;
                if (loginInfo != null)
                {
                    htmlLoginBox =UIHelpers.BuildLoginBoxHtml(loginInfo);

                }
                else
                {
                    HttpCookie cookie = Request.Cookies["SJEDU_UID"];
                    if (cookie != null)
                    {
                        var uid = cookie["UID"];
                        var pwd = cookie["PWD"];

                        var student = Proxy.Login(uid, pwd);
                        if (student != null)
                        {
                            htmlLoginBox = UIHelpers.BuildLoginBoxHtml(student);
                            Session["StudentInfo"] = student;
                        }
                    }
                }
            }
        }
        
    }
}